package event

import (
	"gitchina/CPTT4WeChat/errors"
	"gitchina/CPTT4WeChat/models/handle"
	"strings"
)

type PushEvent struct {
	*WeChatRequestMessage
	Type string
}

func (e *PushEvent) Handle() interface{} {
	return nil
}

func (e *PushEvent) OnParse(params map[string]string) Event {
	var evt Event
	e.Type = params["event"]
	switch strings.ToLower(e.Type) {
	case "click":
		evt = &ClickEvent{PushEvent: e}
	case "scancode_waitmsg":
		evt = &ScanCodeEvent{PushEvent: e}
	}

	if needAuthorized(evt) {
		if _, authOk := handle.Auth(e.FromUserName); authOk {
			evt = evt.OnParse(params)
		} else {
			evt = &ErrorTextEvent{WeChatRequestMessage: e.WeChatRequestMessage, Error: errors.User_Not_Bind}
		}
	}

	return evt.OnParse(params)
}

func needAuthorized(evt Event) bool {
	_, ok := evt.(*ScanCodeEvent)
	return !ok
}
